package com.example.demo.service.impl;

import com.example.demo.entity.DAccessory;
import com.example.demo.mapper.DAccessoryMapper;
import com.example.demo.service.DAccessoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.example.demo.exception.bizException.BizException;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* <p>
*  服务实现类
* </p>
*
* @author cy
* @since 2020-08-01
*/
@Slf4j
@Service
public class DAccessoryServiceImpl extends ServiceImpl<DAccessoryMapper, DAccessory> implements DAccessoryService {

    @Override
    public Page<DAccessory> listDAccessorysByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询dAccessory: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<DAccessory> queryWrapper =  new QueryWrapper<DAccessory>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<DAccessory> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询dAccessory完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public DAccessory getDAccessoryById(int id) {
        log.info("正在查询dAccessory中id为{}的数据",id);
        DAccessory dAccessory = super.getById(id);
        log.info("查询id为{}的dAccessory{}",id,(null == dAccessory?"无结果":"成功"));
        return dAccessory;
    }

    @Override
    public List<DAccessory> getDAccessoryByDId(int id) {
        log.info("正在查询dAccessory中did为{}的数据",id);
        List<DAccessory> list=super.list(new QueryWrapper<DAccessory>().eq("d_id",id));
        log.info("查询Did为{}的附件有{}个",id,list.size());
        return list;
    }

    @Override
    public int insertDAccessory(DAccessory dAccessory) {
        log.info("正在插入dAccessory");
        if (super.save(dAccessory)) {
            log.info("插入dAccessory成功,id为{}",dAccessory.getAccessoryId());
            return dAccessory.getAccessoryId();
        } else {
            log.error("插入dAccessory失败");
            throw new BizException("添加失败");
        }
    }

    @Transactional
    @Override
    public int deleteDAccessoryById(int id) {
        log.info("正在删除id为{}的dAccessory",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的dAccessory成功",id);
            return id;
        } else {
            log.error("删除id为{}的dAccessory失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Transactional
    @Override
    public int updateDAccessory(DAccessory dAccessory) {
        log.info("正在更新id为{}的dAccessory",dAccessory.getAccessoryId());
        if (super.updateById(dAccessory)) {
            log.info("更新d为{}的dAccessory成功",dAccessory.getAccessoryId());
            return dAccessory.getAccessoryId();
        } else {
            log.error("更新id为{}的dAccessory失败",dAccessory.getAccessoryId());
            throw new BizException("更新失败[id=" + dAccessory.getAccessoryId() + "]");
        }
    }

}
